

import pandas as pd
import copy


col_var = [
        'Lat', 'Long',  #####需要保留经纬数据吗，经度纬度可能影响气候？试一下
        #  ################################下面是一组十分重要的变量，我认为他在拟合不同的传染阶段，人为调整爆发期
        'cases/day-(1-1)',  ####################第一天的确诊人数
        'cases/day-(1-7)',  ################从第七天开始统计的滚动确诊每日平均数目
        #     'cases/day-(8-14)',   #################从第八天开始统计的滚动确诊每日平均数目
        #     'cases/day-(15-21)',##################去掉这个最差劲的趋势数据，试一下结果会不会好一点，太长的周期可能不会起作用，换为致命的15-21滚动
        'fatal/day-(1-7)',
        'fatal/day-(8-14)',
        'fatal/day-(15-21)',  #########同理
        'SmokingRate',
        'density',
    ]

col_target2 = 'cases/day'
col_var2 = [
        'Lat', 'Long',
        'cases/day-(1-1)',
        'cases/day-(1-7)',
        'cases/day-(8-14)',
        'cases/day-(15-21)',
        'days_since_10cases'
    ]

def df_aggregation(df, col, mean_range):
        df_new = copy.deepcopy(df)
        col_new = '{}-({}-{})'.format(col, mean_range[0], mean_range[1])  ###############
        df_new[col_new] = 0
        tmp = df_new[col].rolling(mean_range[1] - mean_range[0] + 1).mean()  #################都是每7天滚动求一次均值
        df_new[col_new][mean_range[0]:] = tmp[:-(mean_range[0])]  ##################手动延后时间序列
        df_new[col_new][pd.isna(df_new[col_new])] = 0
        return df_new[[col_new]].reset_index(drop=True)  #####################完全把原来的索引丢弃掉，设立新的数字索引

def do_aggregations(df):
        df = (pd.concat([df, df_aggregation(df, 'cases/day', [1, 1])], axis=1)).reset_index(drop=True)
        df = (pd.concat([df, df_aggregation(df, 'cases/day', [1, 7])], axis=1)).reset_index(drop=True)
        df = (pd.concat([df, df_aggregation(df, 'cases/day', [8, 14])], axis=1)).reset_index(drop=True)
        df = (pd.concat([df, df_aggregation(df, 'cases/day', [15, 21])], axis=1)).reset_index(drop=True)
        df = (pd.concat([df, df_aggregation(df, 'fatal/day', [1, 1])], axis=1)).reset_index(drop=True)
        df = (pd.concat([df, df_aggregation(df, 'fatal/day', [1, 7])], axis=1)).reset_index(drop=True)
        df = (pd.concat([df, df_aggregation(df, 'fatal/day', [8, 14])], axis=1)).reset_index(drop=True)
        df = (pd.concat([df, df_aggregation(df, 'fatal/day', [15, 21])], axis=1)).reset_index(drop=True)
        for threshold in [1, 10, 100]:  ################设立不同的阈值求和
            days_under_threshold = (df['ConfirmedCases'] < threshold).sum()
            tmp = df["day"] - 22 - days_under_threshold
            tmp[tmp < 0] = 0  ###########照顾到22号之前已经爆发的地区，比如中国湖北
            df["days_since_{}cases".format(threshold)] = tmp
        for threshold in [1, 10, 100]:
            days_under_threshold = (df['Fatalities'] < threshold).sum()
            tmp = df['day'].values - 22 - days_under_threshold
            tmp[tmp <= 0] = 0
            df['days_since_{}fatal'.format(threshold)] = tmp
        if df['place_id'][0] == 'China/Hubei':  #################湖北爆发时间比其他地区早，需要特别调整
            df['days_since_1cases'] += 35  # 2019/12/8
            df['days_since_10cases'] += 35 - 13  # 2019/12/8-2020/1/2 assume 2019/12/8+13
            df['days_since_100cases'] += 4  # 2020/1/18
            df['days_since_1fatal'] += 13  # 2020/1/9
        return df
